package com.lcke.common.login;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.extra.spring.SpringUtil;

public class LoginUserCache {


    public static final String TOKEN = "token";

    // 定义缓存超时时间，就是用户有效时间 为 5min
    private static final Long timeout = 5 * 60 * 1000L;
    private static final Cache<String, UserResult> userCache = CacheUtil.newTimedCache(timeout);


    public static Cache<String, UserResult> setUser(String token, UserResult user) {
        userCache.put(token, user);
        return userCache;
    }

    public static UserResult getUser(String token) {
        if (SpringUtil.getProperty("lemon.token").equals(token)) {
            return new UserResult()
                    .setUserName("柠檬")
                    .setUserId("lemon");
        }
        UserResult userResult = userCache.get(token);
        if (null == userResult) {
            return new UserResult();
        }
        return userResult;
    }

}
